<?php
App::uses('AppController', 'Controller');
App::import('Model','User');
/**
 * Users Controller
 *
 * @property User $User
 */
class UsersController extends AppController {

	public function beforeFilter() {
		parent::beforeFilter();
		Controller::loadModel('Parameter');
		Controller::loadModel('Profile');
	 	$this->Auth->authenticate = array(
      'Form' => array (
        'scope' => array('User.email_verified' => User::email_verified, 
												 'User.status' => User::active),
				'fields' => array('username' => 'email')
      )
    );
		#$this->Auth->allow();
		#$this->Auth->deny(array('view'));
		$this->Auth->allow(array('login', 'verify'));
		$this->set('status', $this->Parameter->getUserStatus()); // status array of users
	}


	public function admin_login() {
		  if ($this->request->is('post')) {
		      if ($this->Auth->login()) {
		          $this->redirect($this->Auth->redirect());
		      } else {
		          $this->Session->setFlash(__('Invalid username or password, try again'));
		      }
		  }
	}

	public function admin_logout() {
		  $this->redirect($this->Auth->logout());
	}

	public function verify($token){
		$user = $this->User->findByEmailToken($token);
		if($user){
			$this->Session->setFlash(__('Verify successfully. Please login'));
			$user['User']['email_verified'] =  User::email_verified;
			$user['User']['email_token'] = null;
			$this->User->save($user);
		}else{
			$this->Session->setFlash(__('There is some wrong.<br/> Please contact to adminitrator'));
		}
		$this->redirect(array('action' => 'login'));
	}

/**
 * index method
 *
 * @return void
 */
	public function admin_index() {
		$this->User->recursive = 0;
		$this->set('users', $this->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_view($id = null) {
		$this->User->id = $id;
		if (!$this->User->exists()) {
			throw new NotFoundException(__('Invalid user'));
		}
		$a = $this->User->read(null, $id);
		$this->set('user', $this->User->read(null, $id));
	}

/**
 * add method
 *
 * @return void
 */
	public function admin_add() {
		if ($this->request->is('post')) {
			$this->User->create();
			$this->request->data['Profile']['email'] = $this->request->data['User']['email'];
			if ($this->User->saveAssociated($this->request->data)){
				$this->Session->setFlash(__('The user has been saved'), 'default', array(), 'good');
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.'), 'default', array(), 'bad');
			}
		}
		
		$roles = $this->User->Role->find('list');
		$this->set(compact('roles'));
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_edit($id = null) {
		$this->User->id = $id;
		if (!$this->User->exists()) {
			throw new NotFoundException(__('Invalid user'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->User->saveAssociated($this->request->data)) {
				$this->Session->setFlash(__('The user has been saved'), 'default', array(), 'good');
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.'), 'default', array(), 'bad');
			}
		} else {
			$this->request->data = $this->User->read(null, $id);
		}
		$roles = $this->User->Role->find('list');
		$this->set(compact('roles'));
	}

/**
 * delete method
 *
 * @throws MethodNotAllowedException
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_delete($id = null) {
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->User->id = $id;
		if (!$this->User->exists()) {
			throw new NotFoundException(__('Invalid user'));
		}
		if ($this->User->delete()) {
			$this->Session->setFlash(__('User deleted'), 'default', array(), 'good');
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('User was not deleted'), 'default', array(), 'bad');
		$this->redirect(array('action' => 'index'));
	}
}
